package com.example.controller.before;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Collection;
import com.example.entity.Goods;
import com.example.entity.History;
import com.example.service.CollectionService;
import com.example.service.GoodsService;
import com.example.service.HistoryService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "小程序收藏和历史") //swagger的注释
@CrossOrigin
@RequestMapping("/CollHis")
@RestController
public class CollHisContorller {

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value = "添加收藏")
    @PostMapping(value = "addColl")
    public R addColl(@RequestBody Collection collection) {
        boolean b = collectionService.save(collection);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "查询单个收藏")
    @GetMapping(value = "checkColl/{userId}/{goodsId}")
    public R selectColl(
            @ApiParam(name = "userId", value = "用户id", required = true) @PathVariable String userId,
            @ApiParam(name = "goodsId", value = "物品id", required = true) @PathVariable String goodsId
    ) {
        if (!StringUtils.isEmpty(userId) && !StringUtils.isEmpty(goodsId)) {
            QueryWrapper<Collection> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId);
            wrapper.eq("goods_id", goodsId);
            Collection one = collectionService.getOne(wrapper);
            return R.ok().data("coll", one);
        }
        return R.error().message("userId或商品id为空");
    }

    @ApiOperation(value = "查询多个收藏")
    @GetMapping(value = "checkColl/{userId}/{current}/{limit}")
    public R selectColl(
            @ApiParam(name = "userId", value = "用户id", required = true) @PathVariable String userId,
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit
    ) {
        //判断userId是否有数据
        if (!StringUtils.isEmpty(userId)) {
            Page<Collection> page = new Page<>(current, limit);
            //依赖user_id查询
            QueryWrapper<Collection> wrapper = new QueryWrapper<>();
            wrapper.select("goods_id").eq("user_id", userId);
            collectionService.page(page, wrapper);
            List<Collection> collList = page.getRecords();
            List list = new ArrayList();
            if (collList.size() > 0) {
                for (int i = 0; i < collList.size(); i++) {
                    String goodsId = collList.get(i).getGoodsId();
                    list.add(goodsId);
                }
                java.util.Collection<Goods> goods = goodsService.listByIds(list);
                long total = page.getTotal();

                return R.ok().data("total", total).data("list", goods);
            }
        }
        return R.error().message("userId为空或无数据");
    }

    @ApiOperation(value = "删除收藏")
    @DeleteMapping(value = "deleteColl/{userId}/{goodsId}")
    public R deleteColl(@PathVariable String userId, @PathVariable String goodsId) {
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("goods_id", goodsId);
        boolean b = collectionService.remove(wrapper);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "添加历史")
    @PostMapping(value = "addHis")
    public R addHis(@RequestBody History history) {
        //判断是否已经添加过
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", history.getUserId());
        wrapper.eq("goods_id", history.getGoodsId());
        History one = historyService.getOne(wrapper);
        //未添加过则添加，否则不添加
        if (StringUtils.isEmpty(one)) {
            boolean b = historyService.save(history);
            if (b) {
                return R.ok();
            } else {
                return R.error();
            }
        } else {
            return R.ok();
        }
    }

    @ApiOperation(value = "分页查看历史")
    @GetMapping(value = "checkHis/{userId}/{current}/{limit}")
    public R addHis(
            @ApiParam(name = "userId", value = "用户id", required = true) @PathVariable String userId,
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit
    ) {
        //判断userId是否有数据
        if (!StringUtils.isEmpty(userId)) {
            Page<History> pageUser = new Page<>(current, limit);
            Page<History> page = new Page<>(current, limit);
            //依赖user_id查询
            QueryWrapper<History> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId);
            historyService.page(page, wrapper);
            List<History> HisList = page.getRecords();
            List list = new ArrayList<>();
            if (HisList.size() > 0) {

                for (int i = 0; i < HisList.size(); i++) {
                    String goodsId = HisList.get(i).getGoodsId();
                    list.add(goodsId);
                }
                java.util.Collection<Goods> goods = goodsService.listByIds(list);
                long total = page.getTotal();
                return R.ok().data("total", total).data("list", goods);
            }
        }
        return R.ok().message("userId为空或数据为空");

    }

    @ApiOperation(value = "删除历史")
    @DeleteMapping(value = "deleteHis/{userId}/{goodsId}")
    public R deleteHis(@PathVariable String userId, @PathVariable String goodsId) {
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("goods_id", goodsId);
        boolean b = historyService.remove(wrapper);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "清空历史")
    @DeleteMapping(value = "deleteAllHis/{userId}")
    public R deleteAllHis(@PathVariable String userId) {
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        boolean b = historyService.remove(wrapper);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "清空收藏")
    @DeleteMapping(value = "deleteAllColl/{userId}")
    public R deleteAllColl(@PathVariable String userId) {
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        boolean b = collectionService.remove(wrapper);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}